<?php
/**
 * Created by PhpStorm.
 * User: helei  <dayugog@gmail.com>
 * Date: 2016/12/28
 * Time: 20:24
 */

namespace Payment\Common\Ali\Data\Charge;
use Payment\Common\PayException;

/**
 * 支付宝 条码支付
 *  - 扫户扫用户的二维码，完成支付
 * Class BarChargeData
 *
 * @property string $operator_id  商户操作员编号
 * @property string $terminal_id 商户机具终端编号
 * @property string $alipay_store_id 支付宝店铺的门店ID
 * @property string $scene  条码支付，取值：bar_code 声波支付，取值：wave_code
 * @property string $auth_code 支付授权码 二维码的数值
 *
 * @package Payment\Common\Ali\Data\Charge
 */
class BarChargeData extends ChargeBaseData
{
    /**
     * 业务请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递
     *
     * @return string
     */
    protected function getBizContent()
    {
        $content = [
            'body'          => strval($this->body),
            'subject'       => strval($this->subject),
            'out_trade_no'  => strval($this->order_no),
            'total_amount'  => strval($this->amount),
            'seller_id' => $this->partner,

            'store_id' => $this->store_id,
            'operator_id' => $this->operator_id,
            'terminal_id' => $this->terminal_id,
            'alipay_store_id' => $this->alipay_store_id,
            'scene' => $this->scene,
            'auth_code' => $this->auth_code,
        ];

        $timeExpire = $this->timeout_express;
        if (! empty($timeExpire)) {
            $express = floor(($timeExpire - strtotime($this->timestamp)) / 60);
            $express && $content['it_b_pay'] = $express . 'm';// 超时时间 统一使用分钟计算
        }

        return json_encode($content, JSON_UNESCAPED_UNICODE);
    }

    protected function checkDataParam()
    {
        parent::checkDataParam(); // TODO: Change the autogenerated stub

        $scene = $this->scene;
        $authCode = $this->auth_code;

        if (empty($scene) || ! in_array($scene, ['bar_code', 'wave_code'])) {
            throw new PayException('支付场景 scene 必须设置 条码支付：bar_code 声波支付：wave_code');
        }

        if (empty($authCode)) {
            throw new PayException('请提供支付授权码');
        }
    }
}
